package com.powerbank.user.service.impl;

import cn.hutool.core.util.StrUtil;
import com.powerbank.common.exception.BusinessException;
import com.powerbank.common.result.ResultCode;
import com.powerbank.user.dto.NearbyDeviceQueryDTO;
import com.powerbank.user.mapper.DeviceMapper;
import com.powerbank.user.service.DeviceQueryService;
import com.powerbank.user.vo.DeviceInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备查询服务实现类
 */
@Slf4j
@Service
public class DeviceQueryServiceImpl implements DeviceQueryService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public List<DeviceInfoVO> findNearbyDevices(NearbyDeviceQueryDTO queryDTO) {
        log.info("查询附近设备 - 经度: {}, 纬度: {}, 半径: {}km", 
                queryDTO.getLongitude(), queryDTO.getLatitude(), queryDTO.getRadius());

        List<DeviceInfoVO> devices = deviceMapper.findNearbyDevices(
                queryDTO.getLongitude(),
                queryDTO.getLatitude(),
                queryDTO.getRadius(),
                queryDTO.getDeviceStatus(),
                queryDTO.getOnlyAvailable()
        );

        // 处理设备状态描述和可用性
        devices.forEach(this::processDeviceInfo);

        log.info("找到附近设备数量: {}", devices.size());
        return devices;
    }

    @Override
    public DeviceInfoVO getDeviceDetail(String deviceCode) {
        if (StrUtil.isBlank(deviceCode)) {
            throw new BusinessException(ResultCode.PARAM_ERROR.getCode(), "设备编码不能为空");
        }

        DeviceInfoVO device = deviceMapper.getDeviceDetailByCode(deviceCode);
        if (device == null) {
            throw new BusinessException(ResultCode.DEVICE_NOT_EXIST);
        }

        // 处理设备状态描述和可用性
        processDeviceInfo(device);

        return device;
    }

    @Override
    public boolean isDeviceAvailable(String deviceCode) {
        DeviceInfoVO device = getDeviceDetail(deviceCode);
        return device.getAvailable();
    }

    /**
     * 处理设备信息，添加状态描述和可用性判断
     */
    private void processDeviceInfo(DeviceInfoVO device) {
        // 设置设备状态描述
        String statusDesc = getDeviceStatusDesc(device.getOnlineStatus(), device.getDeviceStatus());
        device.setStatusDesc(statusDesc);

        // 判断设备是否可用
        boolean available = isDeviceUsable(device.getOnlineStatus(), device.getDeviceStatus(), device.getAvailableSlots());
        device.setAvailable(available);
    }

    /**
     * 获取设备状态描述
     */
    private String getDeviceStatusDesc(Integer onlineStatus, Integer deviceStatus) {
        if (onlineStatus == 0) {
            return "设备离线";
        }

        switch (deviceStatus) {
            case 0:
                return "设备停用";
            case 1:
                return "正常运行";
            case 2:
                return "设备故障";
            case 3:
                return "维护中";
            default:
                return "未知状态";
        }
    }

    /**
     * 判断设备是否可用
     */
    private boolean isDeviceUsable(Integer onlineStatus, Integer deviceStatus, Integer availableSlots) {
        // 设备必须在线、状态正常且有可用插槽
        return onlineStatus == 1 && deviceStatus == 1 && availableSlots > 0;
    }
}